// 防止表单回车直接跳转页面
document.onkeydown = function (event) {
    var target, code, tag;
    if (event) {
        target = event.target; //针对遵循w3c标准的浏览器，如Firefox
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") {
                $('.ajax-post').trigger("click");
                return false;
            } else {
                return true;
            }
        }
    }
};

// 表单提交
document.addEventListener('submit', function (event) {
    event.preventDefault();
    //加载层
    var index = layer.load(2, {shade: [0.2, '#000']});

    var url = event.target.action;

    var formData = $('form').serialize();
    $.post(url, formData, function (res) {
        if (res.result == 1) {
            layer.msg(res.msg, {icon: 1, time: 1000});
            setTimeout(function () {
                window.location.href = res.data;
            }, 1500)
        } else if (res.result == 2) {
            layer.close(index);
            layer.msg(res.msg, {icon: -1, time: 1000});
        } else if (res.result == 3) {
            layer.close(index);
            layer.msg(res.msg, {icon: -1, time: 1000});
            setTimeout(function () {
                window.location.href = res.data;
            }, 1000)
        }
    }, 'json');

    setTimeout(function () {
        layer.close(index);
    }, 3000)
    return false;
})

// 返回上一页
$('.back-page').click(function () {
    window.history.go(-1);
})